//
//  MyServiceCellFlowLayout.m
//  PigMarket
//
//  Created by Felix on 2019/3/24.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MyServiceCellFlowLayout.h"

@interface MyServiceCellFlowLayout ()
// 总页数
@property (nonatomic, assign) NSInteger pagesNum;
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes*> *itemsAttributes;

@end

@implementation MyServiceCellFlowLayout

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 每个Cell大小 -1是用作1个像素的间距
        self.itemSize = CGSizeMake((SCREEN_WIDTH - 2 * 12)/3.0, 80);
        // 每行的最小间距
        self.minimumLineSpacing = 0.0f;
        // 每列的最小间距，设置Cell的大小已经留出1个像素大小，所以此处设置0即可
        self.minimumInteritemSpacing = 0.0f;
        // CollectionView视图的/上/左/下/右,的边距
        self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.cellNumsOfPage = 9;
        self.columnOfPage = 3;
    }
    return self;
}
- (void)prepareLayout {
    [super prepareLayout];
    self.itemsAttributes = [NSMutableArray array];
    // 获取每个section的cell个数
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    self.pagesNum = [self getNumberOfPage:count];
    for (int i = 0; i < count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        // 重新排列
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.itemsAttributes addObject:attributes];
    }
}
/** 获取总页数 */
- (NSInteger)getNumberOfPage:(NSInteger)totalCellNumsOfSection {
    if (totalCellNumsOfSection == 0) {
        return 0;
    }
    if (totalCellNumsOfSection > self.cellNumsOfPage) {
        return ceil((double)totalCellNumsOfSection / self.cellNumsOfPage);
    } else {
        return 1;
    }
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(self.collectionView.bounds.size.width * self.pagesNum, [super collectionViewContentSize].height);
//    return CGSizeMake(self.collectionView.bounds.size.width * self.pagesNum, 240);
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return (self.itemsAttributes.count > 0) ? self.itemsAttributes : [super layoutAttributesForElementsInRect:rect];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes*attributes =  [super layoutAttributesForItemAtIndexPath:indexPath];
    // 第几页
    NSUInteger curPage = indexPath.row / self.cellNumsOfPage;
    // 当前cell所在当页的index
    NSUInteger curIndex = indexPath.row - curPage * self.cellNumsOfPage;
    // 当前cell所在当页的行
    NSUInteger curColumn = curIndex % self.columnOfPage;
    // 当前cell所在当页的列
    NSUInteger curRow = curIndex / self.columnOfPage;
    // 调整attributes（大小不变，位置改变）
    CGRect rect = attributes.frame;
    attributes.frame = CGRectMake(self.collectionView.bounds.size.width * curPage + curColumn * self.itemSize.width + curColumn * self.minimumLineSpacing,curRow * self.itemSize.height + curRow * self.minimumInteritemSpacing, rect.size.width, rect.size.height);
    return attributes;
}

@end
